package routers

import (
	"goconfcenter/controllers"
	"goconfcenter/logger"
	"goconfcenter/middlewares"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func InitWebRouter() *gin.Engine {
	var r *gin.Engine
	r = gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true))
	r.GET("/common/conf", (&controllers.CommonController{}).Conf)
	r.GET("/message/count", (&controllers.MessageController{}).Count)
	login := r.Group("/login")
	{
		login.GET("/randomImg", (&controllers.LoginController{}).RandomImg)
		login.POST("/doLogin", (&controllers.LoginController{}).DoLogin)
		login.POST("/userEnter", middlewares.JWTAuthMiddleware(), middlewares.OperationLogMiddleware("用户登录"), (&controllers.LoginController{}).UserEnter)
		login.GET("/getUserInfo", middlewares.JWTAuthMiddleware(), (&controllers.LoginController{}).GetUserInfo)
		login.POST("/doLogout", middlewares.JWTAuthMiddleware(), (&controllers.LoginController{}).DoLogout)
	}
	backendUser := r.Group("/rmsBackendUser", middlewares.JWTAuthMiddleware())
	{
		backendUser.POST("/dataGrid", (&controllers.BackendUserControllers{}).DataGrid)
		backendUser.POST("/accountExists", (&controllers.BackendUserControllers{}).AccountExists)
		backendUser.POST("/add", middlewares.OperationLogMiddleware("添加用户"), (&controllers.BackendUserControllers{}).Add)
		backendUser.GET("/get", (&controllers.BackendUserControllers{}).Get)
		backendUser.POST("/edit", middlewares.OperationLogMiddleware("编辑用户"), (&controllers.BackendUserControllers{}).Edit)
		backendUser.POST("/lock", middlewares.OperationLogMiddleware("锁定用户"), (&controllers.BackendUserControllers{}).Lock)
		backendUser.POST("/unlock", middlewares.OperationLogMiddleware("解锁用户"), (&controllers.BackendUserControllers{}).UnLock)
		backendUser.POST("/delete", middlewares.OperationLogMiddleware("删除用户"), (&controllers.BackendUserControllers{}).Delete)
		backendUser.POST("/roleDataGrid", (&controllers.BackendUserControllers{}).RoleDataGrid)
		backendUser.POST("/authRole", middlewares.OperationLogMiddleware("用户角色授权"), (&controllers.BackendUserControllers{}).AuthRole)
	}
	role := r.Group("/rmsRole", middlewares.JWTAuthMiddleware())
	{
		role.POST("/dataGrid", (&controllers.RoleController{}).DataGrid)
		role.POST("/add", middlewares.OperationLogMiddleware("添加角色"), (&controllers.RoleController{}).Add)
		role.GET("/get", (&controllers.RoleController{}).Get)
		role.POST("/edit", middlewares.OperationLogMiddleware("编辑用户"), (&controllers.RoleController{}).Edit)
		role.GET("/resTreeGrid", (&controllers.RoleController{}).ResTreeGrid)
		role.POST("/auth", middlewares.OperationLogMiddleware("角色资源授权"), (&controllers.RoleController{}).Auth)
	}
	resource := r.Group("/rmsResource", middlewares.JWTAuthMiddleware())
	{
		resource.POST("/treeGrid", (&controllers.ResourceController{}).TreeGrid)
		resource.POST("/updateResourceTree", (&controllers.ResourceController{}).UpdateResourceTree)
		resource.GET("/get", (&controllers.ResourceController{}).Get)
		resource.POST("/edit", (&controllers.ResourceController{}).Edit)
	}
	operationLog := r.Group("/sysOperationLog", middlewares.JWTAuthMiddleware())
	{
		operationLog.POST("/dataGrid", (&controllers.OperationLogController{}).DataGrid)
	}
	r.GET("/", func(c *gin.Context) {
		time.Sleep(5 * time.Second)
		c.String(http.StatusOK, "Welcome Gin Server")
	})
	//注册业务路由
	return r
}
